<?php

namespace app\controller;
use app\service\TaskWorkOrderReportService;
use support\Request;
use support\Response;
use think\db\exception\DataNotFoundException;

/**
 * @title 工单报表相关接口控制层
 */
class TaskWorkOrderReportsController
{

    protected TaskWorkOrderReportService $taskWorkOrderReportService;
    public function __construct()
    {
        $this->taskWorkOrderReportService = TaskWorkOrderReportService::getInstance();
    }


    /**
     * 统计不同环节状态下的工单数量
     * @param Request $request
     * @return Response
     */
    public function countTaskWorkOrderNumberDiffByStatus(Request $request): Response
    {
        //按项目 , 按租户进行统计
        $param = $request->post()['param'];
        $tenantId = $request->getTenantId();
        $projectId = $param['project_id'];
        $createTime = $param['time'];
        $stepCategoryId = $param['step_category_id'] ?? [];

        try {
            $response = $this->taskWorkOrderReportService->countTaskWorkOrderNumberDiffByStatus(
                $tenantId,
                $projectId,
                $createTime,
                $stepCategoryId
            );

        } catch (DataNotFoundException $e) {
            throw_strack_exception($e->getMessage(), -400001);
        }
        return json(success_response('success', $response));
    }

    /**
     * 统计不同工充环节下的工单占比
     * @param Request $request
     * @return Response
     */
    public function countTaskWorkOrderProportion(Request $request): Response
    {
        $param = $request->post()['param'];
        $tenantId = $request->getTenantId();
        $projectId = $param['project_id'] ?? [];
        $createdTime = $param['time'];
        $stepCategoryId = $param['step_category_id'] ?? [];

        try {
            $response = $this->taskWorkOrderReportService->countTaskWorkOrderProportion(
                $tenantId,
                $projectId,
                $createdTime,
                $stepCategoryId
            );
        } catch (DataNotFoundException $e) {
            throw_strack_exception($e->getMessage(), -400001);
        }
        return json(success_response('success', $response));
    }

    /**
     * 工单报表
     * @param Request $request
     * @return Response
     */
    public function getTaskWorkOrderReports(Request $request): Response
    {
        $param = $request->post();
        $tenantId = $request->getTenantId();
        $projectId = $param['param']['project_id'];
        $stepCategoryId = $param['param']['step_category_id'] ?? [];
        $categoryId = $param['param']['category_id'] ?? [];
        $time = $param['param']['time'];
        $page = $param['page'] ?? [1, config("database.database_max_select_rows")];

        try {
            $response = $this->taskWorkOrderReportService->getTaskWorkOrderReports(
                $tenantId,
                $projectId,
                $page,
                $time,
                $stepCategoryId,
                $categoryId
            );
        } catch (DataNotFoundException $e) {
             throw_strack_exception($e->getMessage(), -400001);
        }
        return json(success_response('success', $response));
    }

    /**
     * 按环节统计工单问题数据
     * @param Request $request
     * @return Response
     */
    public function countTaskWorkOrderByStepCategoryId(Request $request) : Response
    {
        $param = $request->post();
        $tenantId = $request->getTenantId();
        $projectId = $param['param']['project_id'];
        $stepCategoryId = $param['param']['step_category_id'] ?? [];
        $time = $param['param']['time'];
        try {
            $response = $this->taskWorkOrderReportService->countTaskWorkOrderByStepCategoryId(
                $projectId,
                $tenantId,
                $time,
                $stepCategoryId,
            );
        }catch (DataNotFoundException $exception){
            throw_strack_exception($exception->getMessage(), -400001);
        }

        return json(success_response('success', $response));
    }

}
